<?php
/**
 * 汽车资讯model
 * @author 王涛
 * @DataTime 2018-08-02T08:46:15+0800
 */
namespace app\api\model;
use think\Model;
class AdvertisementModel extends Model{
    protected $table = 'orange_advertisement';
    protected $insert = ['status' => 0];
    protected $createTime = 'ctime';
    protected $updateTime = 'utime';
    protected $autoWriteTimestamp = true;

    protected function getCtimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }

    protected function getUtimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }
    /**
     * 添加 广告-轮播图
     * @author 王涛
     * @DataTime 2018-08-02T20:28:04+0800
     * @param    [type]                   $addarray [参数数组]
     */
    public function addAd($addarray)
    {
        $result = $this->save($addarray);
        if($result){
            return $this->id;
        }else{
            return false;
        }
    }

    /**
     * 轮播列表重写
     * @param $where
     * @param $pageNum
     * @param $listNum
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenHong~
     */
    public function adList($where,$pageNum,$listNum)
    {
        $data['list'] = $this->alias('AD')
                ->join('__AREAS__ A','A.area_id = AD.province','left')
                ->join('__AREAS__ B','B.area_id = AD.city','left')
                ->join('__AREAS__ C','C.area_id = AD.area','left')
                ->where($where)
                ->field('AD.*,A.area_name province_name,B.area_name city_name,C.area_name area_name')
                ->page($pageNum, $listNum)
                ->order('AD.position asc')
                ->select();
        $data['total'] = $this->alias('AD')
                        ->where($where)
                        ->count("AD.id");
        return $data;
    }

    /**
     * 编辑轮播图-重写
     * @param $addarray
     * @return false|int
     * @author zhenHong~
     */
    public function adEdit($addarray)
    {
        $result = $this->isUpdate(true)->save($addarray);
        return $result;
    }

    /**
     * 删除轮播图 重写
     * @param $id
     * @return int
     * @author zhenHong~
     */
    public function delAd($id)
    {
        $result = $this->where('id',$id)
                        ->setField('status',9);
        return $result;
    }

    /**
     * 查看轮播详情-重写
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author zhenHong~
     */
    public function mationInfo($id)
    {
        $data = $this->find($id);
        return $data;
    }
}
